import "package:ct_pa_majordomo/config/extension.dart";
import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:flutter/material.dart";

class TapContainer extends StatelessWidget {
  final EdgeInsetsGeometry? margin;
  final EdgeInsetsGeometry? padding;
  final double? width;
  final double? height;
  final Clip clipBehavior;
  final BoxDecoration? decoration;
  final TextAlign textAlign;
  final TextStyle? textStyle;
  final bool unconstrained;
  final void Function()? onTap;
  final Widget? child;

  const TapContainer({
    super.key,
    this.margin,
    this.padding,
    this.width,
    this.height,
    this.clipBehavior = Clip.none,
    this.decoration,
    this.textAlign = TextAlign.center,
    this.textStyle,
    this.unconstrained = false,
    this.onTap,
    required this.child,
  });

  @override
  Widget build(BuildContext context) {
    Widget? current = child;
    if (child != null && textStyle != null) {
      current = AnimatedDefaultTextStyle(
        overflow: TextOverflow.ellipsis,
        style: DefaultTextStyle.of(context).style.merge(textStyle),
        textAlign: textAlign,
        duration: Durations.short3,
        child: child!,
      );
    }
    current = AnimatedContainer(
      alignment: Alignment.center,
      margin: margin,
      padding: padding,
      width: width,
      height: height,
      clipBehavior: clipBehavior,
      decoration: decoration,
      duration: Durations.short3,
      child: current,
    );
    if (onTap != null) {
      current = GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: onTap,
        child: current,
      );
    }
    if (unconstrained) current = UnconstrainedBox(child: current);
    return current;
  }
}

class FlatButton extends TapContainer {
  FlatButton({
    super.key,
    super.margin,
    super.padding,
    super.width,
    super.height,
    super.clipBehavior,
    BoxDecoration? decoration,
    super.textAlign,
    TextStyle? textStyle,
    super.unconstrained = true,
    super.onTap,
    required super.child,
  }) : super(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(100),
            color: ThemeVars.colorPrimary,
          ).merge(decoration).copyWith(
                color: onTap == null ? ThemeVars.colorBackground : null,
              ),
          textStyle: const TextStyle(color: Colors.white)
              .merge(textStyle)
              .copyWith(color: onTap == null ? ThemeVars.colorDisabled : null),
        );
}
